****************************
* Replication commands for "Fiscal Federalism at Work? Central Responses to Internal Migration in India", Bhavnani and Lacina, World Development 20xx
****************************

use "BhavnaniLacina_WorldDevelopment_replication.dta", clear
notes

*************
* Global macros
*************
global controlspretreat ADEprecip_avg lpdegrade_avg Lpcprod Lpop_avgnat Lincome_avg Lunemploy_nat LntnlPW_ADEprecip

*******
* Install program ivreg2
*******
*Will install if not already installed:
capture ssc install ivreg2
*Check for updates if already installed:
capture update ivreg2, update

**************
* Summarize all variables (Tables 1 and 6)
**************

summarize

**************
*Figure 1
**************

twoway (scatter  Losm_all LPDW_ADEprecip, msymbol(smcircle) mcolor(black)) (lfit  Losm_all LPDW_ADEprecip), legend(off) title("") note("") ///
ylabel(,valuelabel labs(medium) nogrid) xlabel(,labsize(medium)) xtitle(,size(medium)) ytitle("Ln migrants",size(medium)) name(Fig1, replace)

**********
* Table 3
**********

*OLS
xi: reg Ltotdevres Losm_all $controlspretreat i.statenm [aweight=yrswght], r
	*Wald test
	test Losm_all

*2SLS
xi: ivreg2 Ltotdevres (Losm_all = LPDW_ADEprecip) $controlspretreat i.statenm [aweight=yrswght], first r bw(2)
	*2nd stage Wald test
	test Losm_all
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)

***********
* Table 4
***********
*This code also saves the coefficient estimates in columns 3-6 in matrixes, which will be used to plot Figure 3

*Columns 1 to 3
xi: ivreg2 Ltotdevres (Losm_all Losm_axpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc  i.statenm [aweight=yrswght], first r bw(2)
	mat B1=e(b)
	*Angrist-Pischke F stats
	mat list e(first)
	*Second stage Wald test
	test Losm_all Losm_axpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)

*Column 4: 2nd stage model of grants
xi: ivreg2 Lcentralgrants (Losm_all Losm_axpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc i.statenm [aweight=yrswght], first r bw(2)
	mat B2=e(b)
	*Second stage Wald test
	test Losm_all Losm_axpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
	
*Column 5: 2nd stage model of central scheme funds
*Note that the smaller sample results in different first stage estimates. See also Table 11 in the Appendix.
xi: ivreg2 Lcss (Losm_all Losm_axpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc  i.statenm [aweight=yrswght], first r bw(2)
	mat B3=e(b)
	*Angrist-Pischke F stats reported in Table 11
	mat list e(first)
	*Second stage Wald test
	test Losm_all Losm_axpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)

*Column 6: 2nd stage model of central taxes
xi: ivreg2 Lcentraltaxes (Losm_all Losm_axpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc  i.statenm [aweight=yrswght], first r bw(2)
	mat B4=e(b)
	*Second stage Wald test
	test Losm_all Losm_axpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)

***********
*Figures 2 & 3
***********
capture drop where
capture drop pipe
gen where=0
gen pipe="|"
qui: summ pmatch_cm
mat max = r(max)
mat min = r(min)
qui: xi: ivreg2 Ltotdevres (Losm_all Losm_axpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc  i.statenm [aweight=yrswght], r bw(2)
mat B=e(b)
mat V=e(V)

*Figure 2
twoway (function y = B[1,1] + B[1,2]*x, range(0 1)) ///
(function y = B[1,1] + B[1,2]*x + 1.645*(V[1,1]+x^2*V[2,2]+2*x*V[2,1])^0.5, range(0 1) lpattern("-")) ///
(function y = B[1,1] + B[1,2]*x - 1.645*(V[1,1]+x^2*V[2,2]+2*x*V[2,1])^0.5, range(0 1) lpattern("-")) ///
(scatter where pmatch_cm, ms(none) mlabel(pipe) mlabpos(0) legend(off)), ///
 ytitle("% change in total transfers" "per % change in migration",size(medium)) xtitle("Center-state copartisanship",size(medium)) name(Fig2, replace) ///
 legend(off) ylabel(,valuelabel labs(medium) nogrid) xlabel(,labsize(medium))

*Figure 3
twoway ///
(function y = B1[1,2]*x, range(0 1) lw(thick) lpattern(solid)) ///
(function y = B4[1,2]*x, range(0 1) lpattern(solid)) ///
(function y = B2[1,2]*x, range(0 1) lw(thick) lpattern(dash)) ///
(function y = B3[1,2]*x, range(0 1) lpattern(dash)) ///
(scatter where pmatch_cm, ms(none) mlabel(pipe) mlabpos(0)), ///
ytitle("Additional % increase in funds" "Per % change in migration",size(medium)) xtitle("Center-state copartisanship", size(medium)) name(Fig3, replace) ///
 legend(pos(6) col(2) order(1 "Total transfers" 2 "Central taxes" 3 "Central grants" 4 "Central scheme funds")) ///
 ylabel(,valuelabel labs(medium) nogrid) xlabel(,labsize(medium))
 
***************
* Table 7 (Appendix): Unweighted data
***************

*Columns 1-2
xi: ivreg2 Ltotdevres (Losm_all = LPDW_ADEprecip) $controlspretreat i.statenm, first r bw(2)
	*2nd stage Wald test
	test Losm_all
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
	
*Columns 3-5
xi: ivreg2 Ltotdevres (Losm_all Losm_axpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc  i.statenm, first r bw(2)
	*Angrist-Pischke F stats
	mat list e(first)
	*Second stage Wald test
	test Losm_all Losm_axpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)

***************
* Table 8 (Appendix): Migrants as a percent of the population
***************

*Columns 1-2
xi: ivreg2 Ltotdevres (osm_pc = LPDW_ADEprecip) $controlspretreat i.statenm [aweight=yrswght], first r bw(2)
	*2nd stage Wald test
	test osm_pc
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
		
*Columns 3-5
xi: ivreg2 Ltotdevres (osm_pc osm_pcxpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc i.statenm [aweight=yrswght], first r bw(2)
	mat list e(first)
	*Second stage Wald test
	test osm_pc osm_pcxpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
 
***************
* Table 9 (Appendix): Male migrants
***************

*Columns 1-2
xi: ivreg2 Ltotdevres (Losm_m_avg = LPDW_ADEprecip) $controlspretreat i.statenm [aweight=yrswght], first r bw(2)
	*2nd stage Wald test
	test Losm_m_avg
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
	
*Columns 3-5
xi: ivreg2 Ltotdevres (Losm_m_avg Losm_mxpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc i.statenm [aweight=yrswght], first r bw(2)
	mat list e(first)
	*Second stage Wald test
	test Losm_m_avg Losm_mxpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
	
***************
* Table 10 (Appendix): Urban migrants
***************

*Columns 1-2
xi: ivreg2 Ltotdevres (Losm_u = LPDW_ADEprecip) $controlspretreat i.statenm [aweight=yrswght], first r bw(2)
	*2nd stage Wald test
	test Losm_u
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
	
*Columns 3-5
xi: ivreg2 Ltotdevres (Losm_u Losm_uxpmatch_cm = LPDW_ADEprecip LPDW_ADEprecipxpmatch_cm) pmatch_cm $controlspretreat pmatch_cmlfc pmatch_cmlpc  i.statenm [aweight=yrswght], first r bw(2)
	mat list e(first)
	*Second stage Wald test
	test Losm_u Losm_uxpmatch_cm
	*Anderson-Rubin Chi-Squared
	di e(archi2)
	di e(archi2p)
